

package com.base.commons.tools.redis;

import com.base.commons.tools.redis.interceptor.LockKeyGenerator;
import com.base.commons.tools.redis.serializer.JsonRedisSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis配置
 *
 * @author carl
 * @since 1.0.0
 */
@Configuration
@ConditionalOnProperty(value = "spring.autoconfigure.exclude.redis.open", matchIfMissing = true)
public class RedisConfig {
    @Autowired(required = false)
   private RedisConnectionFactory factory;

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new JsonRedisSerializer<>(Object.class));
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new JsonRedisSerializer<>(Object.class));
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }

    @Bean
    public LockKeyGenerator cacheKeyGenerator() {
        return new LockKeyGenerator();
    }
}